<?php

function new_log($text) {
	$new_log_sql_link = mysql_connect(OPENIDOO_DB_SERVER, OPENIDOO_DB_USER, OPENIDOO_DB_PASSWORD);
	$new_log_select_db = mysql_select_db(OPENIDOO_DB_DATABASE, $new_log_sql_link);
	if ((!$new_log_sql_link) or (!$new_log_select_db)) {
		die('Connection failed.');
	} else {
		if (isset($_SESSION['user_id']) && ($_SESSION['user_id'])) {
			mysql_query("INSERT INTO openidoo_logs (user_id, date, log) VALUES ('".mysql_escape_string($_SESSION['user_id'])."', NOW(), '".mysql_escape_string($text)."')", $new_log_sql_link);
		}
	}
}

function create_assoc_handle() {
	$assoc_handle = hash_hmac('sha1', uniqid(), 'testkey1@idoo');
	return $assoc_handle;
}

function create_secret() {
	$secret = md5(uniqid().'testkey2@idoo');
	return $secret;
}

function create_signed() {

	$signed = array();
	$signed .= $_SESSION;
	if (isset($_SESSION['openid_sreg_required'])) $signed .= $_SESSION['openid_sreg_required'];
	if (isset($_SESSION['openid_sreg_optional'])) $signed .= $_SESSION['openid_sreg_optional'];
	$result = '';

	if (isset($signed['openid_mode'])) {
		$result .= 'mode'.',';
	}
	if (isset($signed['openid_identity'])) {
		$result .= 'identity'.',';
	}
	if (isset($signed['openid_return_to'])) {
		$result .= 'return_to'.',';
	}
	if (isset($signed['openid_sreg_nickname'])) {
		$result .= 'sreg_nickname'.',';
	}
	if (isset($signed['openid_sreg_email'])) {
		$result .= 'sreg_email'.',';
	}
	if (isset($signed['openid_sreg_fullname'])) {
		$result .= 'sreg_fullname'.',';
	}
	if (isset($signed['openid_sreg_dob'])) {
		$result .= 'sreg_dob'.',';
	}
	if (isset($signed['openid_sreg_gender'])) {
		$result .= 'sreg_gender'.',';
	}
	if (isset($signed['openid_sreg_postcode'])) {
		$result .= 'sreg_postcode'.',';
	}
	if (isset($signed['openid_sreg_country'])) {
		$result .= 'sreg_country'.',';
	}
	if (isset($signed['openid_sreg_language'])) {
		$result .= 'sreg_language'.',';
	}
	if (isset($signed['openid_sreg_timezone'])) {
		$result .= 'sreg_timezone'.',';
	}
	if (isset($signed['openid_op_endpoint'])) {
		$result .= 'op_endpoint'.',';
	}
	if (isset($signed['openid_response_nonce'])) {
		$result .= 'response_nonce'.',';
	}
	
	return $result;
}


function create_sig($secret, $signed, $array_token) {
	$token_contents = '';

	$token_contents .= 'mode:id_res\n';

	if ((strpos($signed, 'identity') !== false) && isset($array_token['openid_identity'])) {
		$token_contents .= 'identity:'.$array_token['openid_identity'].'\n';
	}
	if ((strpos($signed, 'return_to') !== false) && isset($array_token['openid_return_to'])) {
		$token_contents .= 'return_to:'.$array_token['openid_return_to'].'\n';
	}
	if ((strpos($signed, 'sreg_nickname') !== false) && isset($array_token['openid_sreg_nickname'])) {
		$token_contents .= 'sreg_nickname:'.$array_token['openid_sreg_nickname'].'\n';
	}
	if ((strpos($signed, 'sreg_email') !== false) && isset($array_token['openid_sreg_email'])) {
		$token_contents .= 'sreg_email:'.$array_token['openid_sreg_email'].'\n';
	}
	if ((strpos($signed, 'sreg_fullname') !== false) && isset($array_token['openid_sreg_fullname'])) {
		$token_contents .= 'sreg_fullname:'.$array_token['openid_sreg_fullname'].'\n';
	}
	if ((strpos($signed, 'sreg_dob') !== false) && isset($array_token['openid_sreg_dob'])) {
		$token_contents .= 'sreg_dob:'.$array_token['openid_sreg_dob'].'\n';
	}
	if ((strpos($signed, 'sreg_gender') !== false) && isset($array_token['openid_sreg_gender'])) {
		$token_contents .= 'sreg_gender:'.$array_token['openid_sreg_gender'].'\n';
	}
	if ((strpos($signed, 'sreg_postcode') !== false) && isset($array_token['openid_sreg_postcode'])) {
		$token_contents .= 'sreg_postcode:'.$array_token['openid_sreg_postcode'].'\n';
	}
	if ((strpos($signed, 'sreg_country') !== false) && isset($array_token['openid_sreg_country'])) {
		$token_contents .= 'sreg_country:'.$array_token['openid_sreg_country'].'\n';
	}
	if ((strpos($signed, 'sreg_language') !== false) && isset($array_token['openid_sreg_language'])) {
		$token_contents .= 'sreg_language:'.$array_token['openid_sreg_language'].'\n';
	}
	if ((strpos($signed, 'sreg_timezone') !== false) && isset($array_token['openid_sreg_timezone'])) {
		$token_contents .= 'sreg_timezone:'.$array_token['openid_sreg_timezone'].'\n';
	}
	if ((strpos($signed, 'op_endpoint') !== false) && isset($array_token['openid_op_endpoint'])) {
		$token_contents .= 'op_endpoint:'.$array_token['openid_op_endpoint'].'\n';
	}
	if ((strpos($signed, 'response_nonce') !== false) && isset($array_token['openid_response_nonce'])) {
		$token_contents .= 'response_nonce:'.$array_token['openid_response_nonce'].'\n';
	}

	$sig = hash_hmac('sha1', $secret, $token_contents);
	$sig = base64_encode($sig);
	return $sig;
}

?>
